package com.xxxx.car.audit.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.car.audit.vo.PackageAuditVo;
import com.xxxx.car.flow.service.IProcessService;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xxxx.car.common.annotation.Log;
import com.xxxx.car.common.core.controller.BaseController;
import com.xxxx.car.common.core.domain.AjaxResult;
import com.xxxx.car.common.enums.BusinessType;
import com.xxxx.car.audit.domain.PackageAudit;
import com.xxxx.car.audit.service.IPackageAuditService;
import com.xxxx.car.common.utils.poi.ExcelUtil;
import com.xxxx.car.common.core.page.TableDataInfo;

/**
 * 审核信息Controller
 * 
 * @author Huang
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/audit/info")
public class PackageAuditController extends BaseController
{
    @Autowired
    private IPackageAuditService packageAuditService;
    @Resource
    private IProcessService processService;

    /**
     * 查询审核信息列表
     */
    @PreAuthorize("@ss.hasPermi('audit:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(PackageAudit packageAudit)
    {
        startPage();
        List<PackageAudit> list = packageAuditService.selectPackageAuditList(packageAudit);
        return getDataTable(list);
    }

    /**
     * 导出审核信息列表
     */
    @PreAuthorize("@ss.hasPermi('audit:info:export')")
    @Log(title = "审核信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PackageAudit packageAudit)
    {
        List<PackageAudit> list = packageAuditService.selectPackageAuditList(packageAudit);
        ExcelUtil<PackageAudit> util = new ExcelUtil<PackageAudit>(PackageAudit.class);
        util.exportExcel(response, list, "审核信息数据");
    }

    /**
     * 获取审核信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('audit:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(packageAuditService.selectPackageAuditById(id));
    }

    /**
     * 获取审核历史信息
     */
    @PreAuthorize("@ss.hasPermi('audit:info:history')")
    @GetMapping(value = "/{id}/histories")
    public AjaxResult historyList(@PathVariable("id") String instanceId)
    {
        return success(processService.selectHistoryTaskListByInstanceId(instanceId));
    }

    /**
     *
     */
    @PreAuthorize("@ss.hasPermi('audit:info:processing')")
    @GetMapping(value = "/{id}/processing")
    public void processing(@PathVariable("id") String instanceId, HttpServletResponse response) {
        try (InputStream stream = processService.getProcessingImageByInstanceId(instanceId)){
            IOUtils.copy(stream,response.getOutputStream());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 新增审核信息
     */
    @PreAuthorize("@ss.hasPermi('audit:info:add')")
    @Log(title = "审核信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult startAudit(@RequestBody PackageAuditVo packageAuditVo)
    {
        return toAjax(packageAuditService.startAudit(packageAuditVo));
    }

    /**
     * 修改审核信息
     */
    @PreAuthorize("@ss.hasPermi('audit:info:edit')")
    @Log(title = "审核信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PackageAudit packageAudit)
    {
        return toAjax(packageAuditService.updatePackageAudit(packageAudit));
    }

    /**
     * 删除审核信息
     */
    @PreAuthorize("@ss.hasPermi('audit:info:remove')")
    @Log(title = "审核信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(packageAuditService.cancelAudit(id));
    }
}
